/*
 *  Copyright (C) 2011 JiangHongTiao <jjurco.sk_gmail.com>
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package sk.lieskove301.jianghongtiao.liaad.prisoner.gene;

/**
 * Date of create: May 15, 2011
 *
 * @author JiangHongTiao <jjurco.sk_gmail.com>
 * @version 2011.0515
 */
public class GeneticInfo implements Comparable<GeneticInfo>{

    private GeneEnum[] gene = new GeneEnum[5];

    public GeneticInfo(GeneEnum[] gene) {
        this.gene = gene;
    }
    
    public GeneticInfo(GeneEnum firstMove, GeneEnum bothCooperate,
            GeneEnum cooperateBetray, GeneEnum betrayCooperate, GeneEnum bothBetray){
        gene[0] = firstMove;
        gene[1] = bothCooperate;
        gene[2] = cooperateBetray;
        gene[3] = betrayCooperate;
        gene[4] = bothBetray;
    }

    public GeneEnum firstMove(){
        return gene[0];
    }

    public GeneEnum bothCooperate(){
        return gene[1];
    }

    public GeneEnum cooperateBetray(){
        return gene[2];
    }

    public GeneEnum betrayCooperate(){
        return gene[3];
    }

    public GeneEnum bothBetray(){
        return gene[4];
    }

    public GeneEnum[] getGene() {
        return gene;
    }

    public void setGene(GeneEnum[] gene) {
        this.gene = gene;
    }

    public GeneEnum get(int index){
        return gene[index % 5];
    }

    public void set(GeneEnum gene, int index){
        this.gene[index] = gene;
    }

    public int compareTo(GeneticInfo o) {
        return this.toString().compareTo(o.toString());
    }

    @Override
    public String toString() {
        String result = "";
        for(int i=0; i<gene.length; i++){
            result += gene[i].value().getLetter();
        }
        return result;
    }

    public GeneticInfo copyGene(){
        GeneEnum[] result = new GeneEnum[5];
        System.arraycopy(gene, 0, result, 0, gene.length);
        GeneticInfo gi = new GeneticInfo(result);
        return gi;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final GeneticInfo other = (GeneticInfo) obj;
        for (int i = 0; i < gene.length; i++) {
            GeneEnum geneEnum = gene[i];
            if(!geneEnum.name().equals(other.get(i).name())){
                return false;
            }
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        for (int i = 0; i < gene.length; i++) {
            GeneEnum geneEnum = gene[i];
            hash += 29*geneEnum.name().hashCode();
        }
        return hash;
    }
    
    

}
